var payTypeStatusData = getStatusData("/paymentOrder/paytype.html");
var bankData = getStatusData("/bankcardController/selectBankcard.html");
var bankInfoOptions = getBankInfoOptions();
bankInfoOptions.data = bankData;
bankInfoOptions.panelWidth=200;
var payTypeDataGridOptions = getPayTypeOptions();
payTypeDataGridOptions.data = payTypeStatusData;
payTypeDataGridOptions.panelWidth=200;

var noteTypeByFIdOptions = getNoteTypeByFIdOptions();
var noteTypeByFIdData;

function getNoteTypeDataGridOptions(index, field){
    var columns = $('#subItem').datagrid("options").columns;
    var rows = $('#subItem').datagrid("getRows"); // 这段代码是// 对某个单元格赋值
    var id = rows[index][columns[0][getColumnIndex(columns[0], "payTypeId")].field];
    noteTypeByFIdOptions.data = getStatusData("/paymentOrder/noteTypeByFid.html?f_paytypeId=" + id);
    noteTypeByFIdOptions.panelWidth=200;
    noteTypeByFIdData = noteTypeByFIdOptions.data;
    return noteTypeByFIdOptions;
}

//付款单子表
//支付类型、票据种类、票据号、票据到期日、付款银行、付款金额、币种、出票日期、支付类型代码、票据种类代码、付款银行代码。
function getPayListSubOptions(editFlag) {
    var initOptions = {
        idField: 'cid',
        fitColumns: false,
        checkOnSelect: false,
        singleSelect: false,
        fit: true,
        columns: [[{
            width: 20,
            checkbox: true
        }, {
            field: 'payType',
            title: '<label class="redSharp">*支付类型</label>',
            width: 80,
            editor: {
                type: 'combogrid',
                options:payTypeDataGridOptions
            }
        }, {
            title: '<label class="redSharp">*票据种类</label>',
            field: 'noteType',
            width: 80,
            editor: {
                type: 'combogrid',
                options: noteTypeByFIdOptions
            }
        }, {
            field: 'noteNumber',
            title: '<label class="redSharp">*票据号</label>',
            width: 110,
            editor: {
                type: 'validatebox'
            }
        }, {
            title: '票据到期日',
            field: 'noteDate',
            width: 80,
            align:'right',
            editor: {
                type: 'datebox'
            }
        }, {
            field: 'payBlank',
            title: '<label class="redSharp">*付款银行</label>',
            width: 110,
            editor: {
                type: 'combogrid',
                options: bankInfoOptions
            }
        }, {
            title: '<label class="redSharp">*付款金额</label>',
            field: 'payAmount',
            width: 60,
            editor: {
                type: 'numberbox',
                options: {
                    precision: 2,
                    required: true,
                    validType: 'noEmptyNumber'
                }
            },
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'currencys',
            title: '币种',
            width: 60,
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                for(var test in currencysData){
                    if(test === value){
                        return currencysData[test];
                    }
                }
            }
        }, {
            title: '出票日期',
            field: 'issueDate',
            width: 80,
            editor: {
                type: 'datebox'
            }
        }, {
            field: 'payCode',
            title: '支付类型代码',
            width: 80
        }, {
            title: '票据种类代码',
            field: 'noteTypeCode',
            width: 80
        }, {
            title: '付款银行代码',
            field: 'payBlankCode',
            width: 80
        } ,{
            hidden:'true',
            title: '付款类型Id',
            field: 'payTypeId',
            width: 80
        }]],
        toolbar: [{
            id: "addSub",
            text: '新增子项',
            iconCls: 'icon-add',
            handler: function () {
                add();
            }
        }, '-', {
            id: "deleteSub",
            text: '删除子项',
            iconCls: 'icon-remove',
            handler: function () {
                del();
            }
        }],
        onClickCell: onClickCell,
        onAfterEdit: function (rowIndex, rowData, changes) {
            calculatePaySub(rowIndex, rowData);
            calculatePayTop();
        }
    }
    return initOptions;
}

var editCellName;
function onClickCell(index, field) {
    if (endEditing($('#subItem'))) {
        editCellName = field;
        if(field==='payType'){
            // 得到columns对象
            var columns = $('#subItem').datagrid("options").columns;
// 得到rows对象
            var rows = $('#subItem').datagrid("getRows"); // 这段代码是// 对某个单元格赋值
            rows[index][columns[0][getColumnIndex(columns[0], "noteTypeCode")].field] = "";
            rows[index][columns[0][getColumnIndex(columns[0], "noteType")].field] = "";
            rows[index][columns[0][getColumnIndex(columns[0], "noteNumber")].field] = "";
            $('#subItem').datagrid('refreshRow', index);
        }else if (field === 'noteType') {
            noteTypeByFIdOptions = getNoteTypeDataGridOptions(index, field);
        }
        $('#subItem').datagrid('selectRow', index)
            .datagrid('editCell', {index: index, field: field});
        var ed = $('#subItem').datagrid('getEditor', {index: index, field: field});
        if (ed && ed.target) {
            $(ed.target).focus();
        }
        editIndex = index;
    }
}

function calculatePaySub(rowIndex, rowData) {
    // 得到columns对象
    var columns = $('#subItem').datagrid("options").columns;
// 得到rows对象
    var rows = $('#subItem').datagrid("getRows"); // 这段代码是// 对某个单元格赋值
    rows[rowIndex][columns[0][getColumnIndex(columns[0], "payCode")].field] = getComboboxTextC(payTypeStatusData, rowData.payType, "payName", "payCode");
    rows[rowIndex][columns[0][getColumnIndex(columns[0], "payTypeId")].field] = getComboboxTextC(payTypeStatusData, rowData.payType, "payName", "id");
    if(noteTypeByFIdData){
        rows[rowIndex][columns[0][getColumnIndex(columns[0], "noteTypeCode")].field] = getComboboxTextC(noteTypeByFIdData, rowData.noteType, "noteName", "noteCode");
        if(editCellName ==="noteType"){
            rows[rowIndex][columns[0][getColumnIndex(columns[0], "noteNumber")].field] = getComboboxTextC(noteTypeByFIdData, rowData.noteType, "noteName", "noteCode");
        }
    }
    rows[rowIndex][columns[0][getColumnIndex(columns[0], "payBlankCode")].field] = getComboboxTextC(bankData.rows, rowData.payBlank, 'bankName','company');

    // 刷新该行, 只有刷新了才有效果
    $('#subItem').datagrid('refreshRow', rowIndex);

}

function preparePaySubmitPostData() {
    endEditing($('#subItem'));
    var payData = {};
    var postData = $('#searchForm-small').serializeJson();
    postData.currencys = $('#currencys').val();
    postData.paidDate = $('#paidDate').datebox("getText");
    //子项表数据
    var subRows = $('#subItem').datagrid('getRows');

    //数据主体结构
    payData.paymentOrder = postData;
    payData.paymentOrderSubs = subRows;
    return payData;
}

function preparePaySubmitRealPostData() {
    var data = preparePaySubmitPostData();
    data.paymentOrder.f_paidId =  $('#payId').text();
    $.each(data.paymentOrderSubs, function (index, value) {
        delete value.cid;
        delete value.payTypeId;
    });
    return data;
}

function validateBottomSubs(data) {
    if (data.length > 0) {
        var str = "";
        $.each(data, function (index, value) {
            var hangNo = index + 1;
            if (isEmpty(value.payType)) {
                str += "第 " + hangNo + " 行" + "支付类型为非空" + "<br/>";
            }else if(value.payType ==="应付票据"){
                if(!value.noteDate){
                    str += "第 " + hangNo + " 行" + "票据到期日为非空" + "<br/>";
                }
                if(!value.issueDate){
                    str += "第 " + hangNo + " 行" + "出票日期为非空" + "<br/>";
                }
            }
            if (isEmpty(value.noteType)) {
                str += "第 " + hangNo + " 行" + "票据种类为非空" + "<br/>";
            }
            if (isEmpty(value.noteNumber)) {
                str += "第 " + hangNo + " 行" + "票据号为非空" + "<br/>";
            }
            if (!isMoreThan0Number(value.payAmount)||value.payAmount<0) {
                str += "第 " + hangNo + " 行" + "付款金额为非空正数" + "<br/>";
            }
            if (isEmpty(value.payBlank)) {
                str += "第 " + hangNo + " 行" + "付款银行为非空" + "<br/>";
            }
        });
        if (str === "") {
            str = "true";
        }
        return str;
    } else {
        return "必须包含有效子项";
    }
}

function initTopPayForm(){
    var data = getStatusData("/paymentOrder/selectPaymentOrderSub.html?id=" + $('#payId').text());
    //appAmount: 150 paidAppNumber: "P1511900062"providerCode: "gpay"providerName: "gpay"
    $('#appAmount').val(data.paymentOrder.appAmount);
    $('#paidAppNumber').val(data.paymentOrder.paidAppNumber);
    $('#providerCode').val(data.paymentOrder.providerCode);
    $('#providerName').val(data.paymentOrder.providerName);
    $('#remark').val("采购合同生成付款申请,自动提交");
    $('#providerName').val(data.paymentOrder.providerName);
    $('#paidDate').datebox({
        required: true,
        editable: false
    });
    $('#paidDate').datebox("setValue", myformatter(new Date()));

}

function initBottomPayDataGrid(){
    initDataGridNoPager($('#subItem'), getPayListSubOptions());
}

//为$('#subItem') 增加子项

function add() {
    $('#subItem').datagrid('uncheckAll');
    var row = {
        cid: UUID(),
        payType:payTypeStatusData[0].payName,
        payCode:payTypeStatusData[0].payCode,
        payTypeId:payTypeStatusData[0].id,
        currencys:"0"
    };
    $('#subItem').datagrid('appendRow', row);
    $('#subItem').datagrid('checkRow', $('#subItem').datagrid('getRows').length - 1);
}

//删除子项
function del() {
    var rows = $('#subItem').datagrid('getChecked');
    var ids = [];
    if (rows.length > 0) {
        $.each(rows, function (index, value) {
            ids.push(value.cid);
        });
        if (ids.length > 0) {
            $.messager.confirm('请确认', '您要删除当前所选项目？', function (r) {
                if (r) {
                    $.each(ids, function (index, value) {
                        $('#subItem').datagrid('deleteRow', $('#subItem').datagrid('getRowIndex', value));
                    });
                    calculatePayTop();
                }
            });
        }
    } else {
        $.messager.alert('提示', '请选择要删除的记录！', 'error');
    }
}

